
!****************************************************************
!*** This program opens and sets port 2 to accept input from  ***
!*** a barcode reader. The operator is then prompted to scan  ***
!*** in the board serial number , the IEEE address and the    ***
!*** board revision                                           ***
!****************************************************************
program barscan

declare
  string ieeeadr
  string sernum
  global numeric revnum
  global string pf_flag
  !persistent numeric cntrl_all                   remove commment **********
  string rev
  string port_no = "/port1"
  numeric wintemp
  global numeric t1i
  string char
  string char2
  string ieeevendor=""
  global numeric global_fflag = 0
  string array [1:12] ieeeascii
  string array [1:8] serascii
  string array [1:2] revascii
  global numeric array [1:12] ieeenumeric
  global numeric array [1:8] sernumeric
end declare


pf_flag = "PASSED"
 global_fflag = 0

wintemp = open device "/term2/win",as "output",xorg 28,yorg 9,xdim 52,ydim 15,border "SCAN"




port2 = open device port_no, speed 9600,bits 7,stop 1,parity "even",stall "off",cts "off",autolf "off"
print using "\1B[2;2HScan in board IEEE address: ",on wintemp
 input on port2, ieeeadr
print using "\1B[2;30H ?#",on wintemp,ieeeadr
!**** close the port to clear the scan buffer ****
close (port2)

ieeelength = len ieeeadr
!**** check that the IEEE address is 12 digits long ****
if ieeelength <> 12 then
   !**** scan IEEE address again ****
   print using "\1B[3;2HBAD IEEE address try again : ",on wintemp
   port2 = open device port_no, speed 9600,bits 7,stop 1,parity "even",stall "off",cts "off",autolf "off"
   input on port2, ieeeadr
   print using "\1B[3;30H ?#",on wintemp,ieeeadr
   close (port2)
end if

ieeelength = len ieeeadr

if ieeelength <> 12 then
   print using "\1B[4;2HBAD IEEE adress",on wintemp
   global_fflag = 1
   pf_flag = "FAILED"
end if


if global_fflag <> 1 then
        !**** convert IEEE string into 12 element string array ****
        for x = 1 to 12
          ieeeascii[x]=mid(ieeeadr,13-x,1)
        next
        !**** convert string array into 12 decimal values ****
        for x = 1 to 12
          ieeenumeric[x]=val(ieeeascii[x],16)
        next
        !**** check the IEEE address for the correct vendor ****
        !**** number in the last 6 digits    ****
        for x = 1 to 6
          ieeevendor = ieeevendor + ieeeascii[13-x]
        next
        if ieeevendor <> "0080B2" then
          print using "\1B[4;2HBAD IEEE adress: digits 7 to 12 must be 0080B2",on wintemp
          global_fflag = 1
          pf_flag = "FAILED"
        end if
end if


if global_fflag <> 1 then

 port2 = open device port_no, speed 9600,bits 7,stop 1,parity "even",stall "off",cts "off",autolf "off"
 print using "\1B[6;2HScan in board serial number: ",on wintemp
 input on port2, sernum
 print using "\1B[6;30H ?#",on wintemp,sernum
 close (port2)

 serlength = len sernum

!**** check that the board serial number is 8 digits long ****
if serlength <> 8 then
   port2 = open device port_no, speed 9600,bits 7,stop 1,parity "even",stall "off",cts "off",autolf "off"
   print using "\1B[7;2HBAD serial number try again: ",on wintemp
   input on port2, sernum
   print using "\1B[7;30H ?#",on wintemp,sernum
   close (port2)
end if

 serlength = len sernum

if serlength <> 8 then
   print using "\1B[8;2HBAD serial number",on wintemp
   global_fflag = 1
   pf_flag = "FAILED"
end if

if global_fflag <> 1 then

!**** convert serial number string into 8 element string array ****
for x = 1 to 8
  serascii[x]=mid(sernum,9-x,1)
next
!**** convert string array into 8 decimal values ****
for x = 1 to 8
  sernumeric[x]=val(serascii[x],16)
next

 end if
end if






if global_fflag <> 1 then

 port2 = open device port_no, speed 9600,bits 7,stop 1,parity "even",stall "off",cts "off",autolf "off"
 print using "\1B[10;2HScan in board REV: ",on wintemp
 input on port2, rev
 print using "\1B[10;30H ?#",on wintemp,rev
 close (port2)

 revlength = len rev

!**** check that the board REV number is 1 digit long ****
if revlength <> 1 then
 port2 = open device port_no, speed 9600,bits 7,stop 1,parity "even",stall "off",cts "off",autolf "off"
 print using "\1B[11;2HBAD REV try again: ",on wintemp
 input on port2, rev
 print using "\1B[11;30H ?#",on wintemp,rev
 close (port2)
end if

 revlength = len rev

if revlength <> 1 then
   print using "\1B[12;2HBAD REV ",on wintemp
   global_fflag = 1
   pf_flag = "FAILED"
end if

  if global_fflag <> 1 then

  !**** convert ASCII charecters into numeric HEX nibbles ****
  revnum = ( ascii(rev) & $F ) - 1

 end if
end if






print using "\1B[13;2HPress any key to exit",on wintemp
loop while poll (t1i,"input")
        input on t1i, char

end loop
loop until (poll(t1i,"input")) = 1
end loop




close (wintemp)


end program
